# -*- coding: utf-8 -*-
import codecs
import sys,os
#from google import *

import Levenshtein

__all__ = ["readSample","load_KBaseKey","findSimilarKeys","LoadCDB","checkTypeRL","isPunctuation"]
 

def LoadCDB(work_file,bspitr=True,bsplit_tab=True):
    fwd=codecs.open(work_file,'r',encoding='utf-8')
    #fwd=codecs.open('D:/data/CDT7/test.conll06','r',encoding='UTF8')
    raw=fwd.read()
    fwd.close()
    if bspitr:
        lines=raw.split('\r\n')
    else:
        lines=raw.split('\n')
    
    corpor=[]
    line_vec=[]
    for ln in lines:
        word=ln.split()
            
        words=[ ''.join(wd.split('\n')) for wd in word if len(wd)>0]
        if len(words)==0 and len(line_vec)>0:
            if len(line_vec)>=1:
                corpor.append(line_vec)
            else:
                line_vec=[]
                continue
                #print 'error line_vec:'+'\t'.join(line_vec[0])
                
            line_vec=[]
        else:
            line_vec.append(words)
    return corpor


def findSimilarKeys(key,key_lst,nbest):
    res_lst = []
    for itm in key_lst:
        sv = Levenshtein.ratio( itm, key )
        res_lst.append((sv,itm))
        if len(res_lst)>nbest+50:
            res_lst = sorted(res_lst,reverse=True)[0:nbest]
    
    res_lst = sorted(res_lst,reverse=True)[0:nbest]
    
    ret_lst= []
    for itm in res_lst:
        ret_lst.append(itm[1])
    return ret_lst



def extract_xml_attrnode(line):
    tmp1=line.split('>')
    idx = tmp1[0].find('=')
    tmp_id = tmp1[0][idx+1:].replace('"','').strip()
    idx = tmp1[1].find('<')
    tmp_name = tmp1[1][0:idx]
    return (tmp_id,tmp_name)

def extract_xml_node(line):
    tmp1=line.split('>')
    idx = tmp1[1].find('<')
    tmp_name = tmp1[1][0:idx]
    return tmp_name

def extract_xml_att(line):
    tmp1=line.split('>')
    idx = tmp1[0].find('=')
    tmp_id = tmp1[0][idx+1:].replace('"','').strip()
    return tmp_id

'''
<weibo id = "wanyuangongjijin7">
    <content>【兰州石化被曝最高每月为员工缴万元公积金】近日，一份名为《兰州石化党办2011年度职工住房公积金明细账》的文件显示，名单上52人中，兰州石化为其缴存公积金超过9万元的有10人，缴存额最高达12万元，平均每月一万。网友纷纷吐槽人家公积金比他一个月收入都要多。http://t.cn/zTwmi9i 有钱人……</content>
    <name id = "1">兰州石化</name>
    <startoffset id = "1">66</startoffset>
    <endoffset id = "1">70</endoffset>
    <kb id = "1">NIL</kb>
</weibo>
'''
def readSample(kb_file):
    fid = codecs.open(kb_file, 'r', encoding='utf-8')
    raw=fid.read()
    fid.close()
    lines = raw.split('\n')
    
    gquery_set = []
    gquery_id = ''
    gquery_content = ''
    gquery_entity_set= []
    gtmp_id = ''
    gtmp_name = ''
    gtmp_position = ['','']
    gtmp_kbid= ''
    for ilx in range(0,len(lines)):
        line = lines[ilx].strip()
        if line[0:6]=='<?xml ':
            continue
        
        if line[0:6]=='<weibo':
            gquery_id = extract_xml_att(line)
            
        if line[0:8]=='</weibo>':
            query = (gquery_id,gquery_content,gquery_entity_set)
            gquery_set.append(query)
            gquery_id = ''
            gquery_content = ''
            gquery_entity_set= []
        
        if line[0:5]=='<name':
            (gtmp_id,gtmp_name)=extract_xml_attrnode(line)
            
        if line[0:12]=='<startoffset':
            (tmp_id1,tmp_off1)=extract_xml_attrnode(line)
            assert(tmp_id1==gtmp_id)
            gtmp_position[0] = tmp_off1
        
        if line[0:10]=='<endoffset':
            (tmp_id1,tmp_off1)=extract_xml_attrnode(line)
            assert(tmp_id1==gtmp_id)
            gtmp_position[1] = tmp_off1
        
        if line[0:3]=='<kb':
            (tmp_id1,gtmp_kbid)=extract_xml_attrnode(line)
            assert(tmp_id1==gtmp_id)
            entity = (gtmp_id,gtmp_name,gtmp_position,gtmp_kbid)
            gquery_entity_set.append(entity)
            
            gtmp_id = ''
            gtmp_name = ''
            gtmp_position = ['','']
            gtmp_kbid= ''
            
        if line[0:9]=='<content>':
            gquery_content = extract_xml_node(line)
            
    return gquery_set
        
def load_KBaseKey(kb_file):
    fid = codecs.open(kb_file, 'r', encoding='utf-8')
    raw = fid.read()
    fid.close()
    lines = raw.split('\n')
    
    map_key2name ={}
    map_name2key={}
    name_list=[]
    for line in lines:
        if not line:
            continue
        item = line.split('",')
        key  = item[0].replace('"','').strip()
        name = item[1].replace('"','').strip()
        
        #assert( map_key2name.get(key) is None)
        #map_key2name[key] = name
        if map_key2name.get(key) is None:
            map_key2name[key] = [name]
        else:
            map_key2name[key].append(name)
            print 'Warning:key--muti_names\t:',key.encode('gbk','ignore'),'--',('\t'.join(map_key2name[key])).encode('gbk','ignore')
        
        
        if map_name2key.get(name) is None:
            map_name2key[name] = [key]
        else:
            map_name2key[name].append(key)
            print 'Warning:name--muti_keys\t:',name.encode('gbk','ignore'),'--',('\t'.join(map_name2key[name])).encode('gbk','ignore')
        
        name_list.append(name)
        
    return (map_key2name,map_name2key,name_list)
#kb_path = 'E:\\desktop\\wu-request\\NLPCC 2014 Shared Tasks Guidelines\\Chinese Entity Linking  SAMPLE DATA NLPCC2014_EL_sample\\'
#kb_file = kb_path+'sample_query_2014.txt.xml'
#query_set = readSample(kb_file)

const_number  =['1','2','3','4','5','6','7','8','9','0','１','２','３','４','５','６','７','８','９','０']
const_number1 =['一','二','三','四','五','六','七','八','九','十','零','两','双','○','几','廿']
const_dot     =['·','％','．','点','∶','‰','%']
const_cchar    =['个','十','百','千','万','亿']
const_operator=['+','-','*','/','／','—','－','＊','＋','×']

const_char    =['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','X','T','U','V',
                'W','S','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
                's','t','u','v','w','x','y','z']
const_char_ch =['Ａ','Ｂ','Ｃ','Ｄ','Ｅ','Ｆ','Ｇ','Ｈ','Ｉ','Ｊ','Ｋ','Ｌ','Ｍ','Ｎ','Ｏ','Ｐ','Ｑ','Ｒ',
                'Ｓ','Ｔ','Ｕ','Ｖ','Ｗ','Ｘ','Ｙ','Ｚ','ａ','ｂ','ｃ','ｄ','ｅ','ｆ','ｇ','ｈ','ｉ','ｊ',
                'ｋ','ｌ','ｍ','ｎ','ｏ','ｐ','ｑ','ｒ','ｓ','ｔ','ｕ','ｖ','ｗ','ｘ','ｙ','ｚ']

const_dateE    =['年','月','日','天','分','时']

const_punctuation=[u'、',u'！',u'。',u'％',u'×',u'）',u'（',u'》',u'《',u'，',u'』',u'『',u'〕',
            u'—',u'’',u'‘',u'；',u'：',u'”',u'“',u'？',u'——',u'－－－',u'〉',u'▲',u'·',u'〈',
            u'※',u'＊',u'○',u'●',u'／',u'……',u'③',u'－－',u'———',u'〔']


def iniConst(cnv):
    for ids in range(len(cnv)):
        if len(cnv[ids])>1:
            cnv[ids]= cnv[ids].decode('utf-8')

iniConst(const_number)
iniConst(const_number1)
iniConst(const_dot)
iniConst(const_cchar)
iniConst(const_dateE)
iniConst(const_char)
iniConst(const_char_ch)
iniConst(const_operator)
    
def isNumber(strv):
    #３８８·７４    ２０４７６亿    ２．１万      ３７．２％     七八
    if strv.find(u'分之')!=-1:
        strv=strv.replace(u'分之', u'%%')
        
    for ids in range(len(strv)):
        ch = strv[ids]
        if not( (ch in const_number) or (ch in const_number1) or (ch in const_cchar) or (ch in const_dot) or (ch in const_operator) ):
            return False
    return True
        
def isAnsiStr(strv):
    #ＤＳ９７    ＡＢＴ—５９４
    if strv[0]==u'第':
        strv = strv[1:]
    
    for ids in range(len(strv)):
        ch = strv[ids]
        if not( (ch in const_number) or (ch in const_number1) or (ch in const_cchar) or  (ch in const_char) 
                or (ch in const_char_ch) or (ch in const_operator) or (ch in const_dot) ):
            return False
    return True

def isDate(strv):
    #２００１年     十九日    星期
    bFindDate = False
    if strv[-1] in const_dateE:
        strv=strv[0:-2]
        bFindDate = True
    if strv[0:2]==u'星期':
        strv = strv[2:]
        bFindDate = True
    if strv[-2:]==u'月份':
        strv = strv[0:-3]
        bFindDate = True    
    if strv[-2:]==u'点半':
        strv = strv[0:-3]
        bFindDate = True      

    for ids in range(len(strv)):
        ch = strv[ids]
        if not( (ch in const_number) or (ch in const_number1) ):
            return False
    return bFindDate

def hasNumber(strv):
    sz = len(strv)
    for ids in range(len(strv)):
        ch = strv[ids]
        if  (ch in const_number) or (ch in const_number1)  or (ch in const_cchar):
            return True
    return False

def IsEnglish(strv):
    for ids in range(len(strv)):
        ch = strv[ids]
        if  (ch in const_char):
            return True
    return False

def checkTypeRL(strv):
    if isNumber(strv):
        return 'number'
    if isDate(strv):
        return 'date'
    if isAnsiStr(strv):
        return 'foreign' 
    return 'special'

def isPunctuation(strv):
    if len(strv)>1:
        return (False,-1)
    if strv in const_punctuation:
        return (True,const_punctuation.index(strv))
    return (False,-1)


        